﻿#include "helicopter.h"

Helicopter::Helicopter(QWidget *parent,QString x) :
    QLabel(parent)
{

    this->pixmap.load(x);
    // 将图片设置到 QLabel 中
    this->setPixmap(pixmap);
    this->setFixedSize(pixmap.size()*0.08);
    // 如果你想自动缩放图片以适应 QLabel 大小，你可以使用 setScaledContents
    this->setScaledContents(true);
    generatePlaneMarker();
    udpSocket = new QUdpSocket(this);

    udpSocket->bind(QHostAddress::Any,8091);
    connect(udpSocket,&QUdpSocket::readyRead,this,&Helicopter::dealMsg);
    textEdit=new QTextEdit(this);
    lineEdit=new QLineEdit(this);
}

void Helicopter::generatePlaneMarker()
{
    PlaneMarker *p=new PlaneMarker(this,":/image/planeMarker.png");
    this->markerVector.append(p);
}

void Helicopter::on_pushButton_s_clicked()
{
    if(lineEdit->text().isEmpty())
    {
        qDebug() << "未选择无人机";
        return;
    }
    QString ip = lineEdit->text();
    quint16 port = 9000;
    //read
    QString str="south";
    //send
    udpSocket->writeDatagram(str.toUtf8(),QHostAddress(ip),port);
}

void Helicopter::dealMsg()
{
    QByteArray datagram;
    datagram.resize(udpSocket->pendingDatagramSize());
    QHostAddress sender;
    quint16 senderPort;
    // 读取接收到的数据
    udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

    // 处理接收到的数据
    QDataStream stream(datagram);
    float electricity,x,y,z;
    QString localIpAddress;
    stream>>electricity>>x>>y>>z>>localIpAddress;
    if(!droneIp.contains(localIpAddress)){
        FriendDrone *f=new FriendDrone(this,":/image/friendDrone.png");
        f->electricity=electricity;f->x=x;f->y=y; f->z =z;f->IpAddress=localIpAddress;
        f->number=++droneNumber;
        responseMap.insert(droneNumber,f);
        droneIp.insert(localIpAddress);
        emit initResponseDrone();
    }
    // 在这里可以对数据进行处理

    QString str = QString("无人机电量: %1 无人机坐标: %2 %3 %4 无人机IP地址: %5")
            .arg(electricity)
            .arg(x)
            .arg(y)
            .arg(z)
            .arg(localIpAddress);
    textEdit->append(str);
}

void Helicopter::on_pushButton_find_clicked()
{
    QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    QString localIpAddress;

    // Find the local IPv4 address
    for (int i = 0; i < ipAddressesList.size(); ++i) {
        if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address()) {
            localIpAddress = ipAddressesList.at(i).toString();
            break; // Use the first IPv4 address found
        }
    }

    // Calculate the broadcast address for the local subnet
    QHostAddress localSubnetBroadcast(localIpAddress);
    QHostAddress subnetMask("255.255.255.0"); // Adjust the subnet mask as needed

    localSubnetBroadcast.setAddress(localSubnetBroadcast.toIPv4Address() | ~subnetMask.toIPv4Address());
    qDebug() << "Broadcast Address: " << localSubnetBroadcast.toString();
    quint16 port = 9000; // Your desired port number

    // Sending the broadcast message to the local subnet
    udpSocket->writeDatagram(localIpAddress.toUtf8(), localSubnetBroadcast, port);
}
